<?php
/**
 * Class to create model edit in backend
 * @author muinx
 */
class CreateBackendModelEdit
{
	protected $model;
	
	function __construct($model)
	{
		$this->model = $model;
	}
	
	/**
	 * Process model content
	 * @param String $content		Content to process
	 * @param String $filename		File name
	 * @param Object $component		Component Object
	 * @return String	Content was replaced
	 */
	function create($content, $filename, $component)
	{
		$model = $this->model;
		
		$comName = str_replace(' ', '_', strtolower($component->name));
		$componentPrefix = str_replace(' ', '_', ucwords($component->name));
		
		$modelFormName 	= null;
		$search 		= array();
		$replace 		= array();
		
		$search = array('#x#', '#author#', '#to_copy#', '#component_prefix#', '#X_no_s#', '#TO_COPY#', '#X#');
		$replace = array($filename, $component->author, $comName, $componentPrefix, ucfirst($filename), strtoupper($comName), strtoupper($filename));
		
		//get filter
		$filter = unserialize($model->fields_to_filter);
		
		//var_dump($filter);
		
		if(isset($filter['category']) && $filter['category'])
		{
			$catId = $filter['catid'];
			
			$search[] = '#FUNCTION_CAN_DELETE#';
			$replace[] = $this->getFucntionCanDelete($comName, $catId);
			
			$search[] = '#FUNCTION_CAN_EDIT_STATE#';
			$replace[] = $this->getFucntionCanEditState($comName, $catId);
			
			$search[] = '#REORDER_CONDITION#';
			$replace[] = '$condition[] = \''.$catId.' = \'. (int) $table->'.$catId.';';
			
			$search[] = '#FIELD_CATEGORY_ID#';
			$replace[] = $catId;
		}
		else 
		{
			$search[] = '#FUNCTION_CAN_DELETE#';
			$replace[] = null;
			
			$search[] = '#FUNCTION_CAN_EDIT_STATE#';
			$replace[] = null;
			
			$search[] = '#REORDER_CONDITION#';
			$replace[] = null;
		}
				
		$content = str_replace($search, $replace, $content);
		
		return $content;
	}
	
	/**
	 * Tempate function canDelete in model
	 * @param String $comName	Component name
	 * @param Int $catId		Category ID
	 */
	private function getFucntionCanDelete($comName, $catId)
	{
		$str = '/**
			 * Method to test whether a record can be deleted.
			 *
			 * @param	object	A record object.
			 * @return	boolean	True if allowed to delete the record. Defaults to the permission set in the component.
			 * @since	1.6
			 */
			protected function canDelete($record)
			{
				if (!empty($record->id)) {
					if ($record->state != -2) {
						return ;
					}
					$user = JFactory::getUser();
		
					if (!empty($record->catid)) {
						return $user->authorise(\'core.delete\', \'com_'.$comName.'.category.\'.(int) $record->'.$catId.');
					}
					else {
						return parent::canDelete($record);
					}
				}
			}';
	
		return $str;
	}
	
	/**
	 * Tempate function canEditState in model
	 * @param String $comName	Component name
	 * @param Int $catId		Category ID
	 */
	private function getFucntionCanEditState($comName, $catId)
	{
		$str = '/**
			 * Method to test whether a record can have its state changed.
			 *
			 * @param	object	A record object.
			 * @return	boolean	True if allowed to change the state of the record. Defaults to the permission set in the component.
			 * @since	1.6
			 */
			protected function canEditState($record)
			{
				$user = JFactory::getUser();
		
				// Check against the category.
				if (!empty($record->catid)) {
					return $user->authorise(\'core.edit.state\', \'com_'.$comName.'.category.\'.(int) $record->'.$catId.');
				}
				// Default to component settings if category not known.
				else {
					return parent::canEditState($record);
				}
			}';
		
		return $str;
	}
}